﻿
using Catalog.API.Expections;

namespace Catalog.API.Products.DeleteProduct
{
    public record DeleteProductResult(bool IsSuccessed);
    public record DeleteProductCommand(Guid Id):ICommand<DeleteProductResult>;
    internal class DeleteProductCommandHandler(IDocumentSession session)
        : ICommandHandler<DeleteProductCommand, DeleteProductResult>
    {
        public async Task<DeleteProductResult> Handle(DeleteProductCommand command, CancellationToken cancellationToken)
        {
           
            var product=await session.LoadAsync<Product>(command.Id,cancellationToken);
            if(product is null)
            {
               
                throw new ProductNotFoundException(command.Id.ToString());
            }
            session.Delete(product);
            await session.SaveChangesAsync(cancellationToken);

             return new DeleteProductResult(true);
        }
    }
}
